/*
 * @Author: Author && Email
 * @Date: 2024-03-15 22:42:04
 * @LastEditors: Author && Email
 * @LastEditTime: 2024-03-17 18:40:12
 * @FilePath: \MDK-ARMd:\WorkSpace\PRJ\Washing_machine\software\Washing_machine_V1\User\moudle\motor\motor_run.c
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
#include "motor_run.h"

motor_t	g_motor_drain;	/* 排水电机 */
motor_t	g_motor_drum;	/* 注水电机 */

/**
 * @description: 电机初始化
 * @return {*}
 */
void motor_hard_init(void)
{
	IO_MOTOR_ENABLE(1);
	memset(&g_motor_drain, 0, sizeof(g_motor_drain));
	memset(&g_motor_drum, 0, sizeof(g_motor_drum));
}

/**
 * @description: 电机运行任务
 * @return {*}
 */
void motor_hard_task(void)
{
	static uint8_t init_flag = 0;
	if(init_flag == 0)
	{
		init_flag = 1;
		motor_hard_init();	/* 电机接口层相关变量初始化 */
	}
	else
	{
		/* 排水泵 */
		if(g_motor_drain.short_break != 0)	/* 刹车 */
		{
			g_motor_drum.running = 0;
			GPIO_WRITE(IO_MOTOR_AIN1_PORT, IO_MOTOR_AIN1_PIN, IO_SET);
			GPIO_WRITE(IO_MOTOR_AIN2_PORT, IO_MOTOR_AIN2_PIN, IO_SET);
		}
		else
		{
			if(g_motor_drain.running != 0)	/* 运行 */
			{
				bsp_pwm_on(PWM_CHANNEL1);
				bsp_pwm_set_cmp(PWM_CHANNEL1, g_motor_drain.duty);
				if (g_motor_drain.direction)
				{
					GPIO_WRITE(IO_MOTOR_AIN1_PORT, IO_MOTOR_AIN1_PIN, IO_RESET);
					GPIO_WRITE(IO_MOTOR_AIN2_PORT, IO_MOTOR_AIN2_PIN, IO_SET);
				}
				else
				{
					GPIO_WRITE(IO_MOTOR_AIN1_PORT, IO_MOTOR_AIN1_PIN, IO_SET);
					GPIO_WRITE(IO_MOTOR_AIN2_PORT, IO_MOTOR_AIN2_PIN, IO_RESET);
				}
			}
			else		/* 电机停止 */
			{
				bsp_pwm_set_cmp(PWM_CHANNEL1, 0);
				bsp_pwm_off(PWM_CHANNEL1);
			}
		}
		/* 滚筒电机 */
		if(g_motor_drum.short_break != 0)	/* 刹车 */
		{
			g_motor_drum.running = 0;
			GPIO_WRITE(IO_MOTOR_BIN1_PORT, IO_MOTOR_BIN1_PIN, IO_SET);
			GPIO_WRITE(IO_MOTOR_BIN2_PORT, IO_MOTOR_BIN2_PIN, IO_SET);
		}
		else
		{
			if(g_motor_drum.running != 0)	/* 运行 */
			{
				bsp_pwm_on(PWM_CHANNEL2);
				bsp_pwm_set_cmp(PWM_CHANNEL2, g_motor_drum.duty);
				if (g_motor_drum.direction)
				{
					GPIO_WRITE(IO_MOTOR_BIN1_PORT, IO_MOTOR_BIN1_PIN, IO_SET);
					GPIO_WRITE(IO_MOTOR_BIN2_PORT, IO_MOTOR_BIN2_PIN, IO_RESET);
				}
				else
				{
					GPIO_WRITE(IO_MOTOR_BIN1_PORT, IO_MOTOR_BIN1_PIN, IO_RESET);
					GPIO_WRITE(IO_MOTOR_BIN2_PORT, IO_MOTOR_BIN2_PIN, IO_SET);
				}
			}
			else		/* 电机停止 */
			{
				bsp_pwm_set_cmp(PWM_CHANNEL2, 0);
				bsp_pwm_off(PWM_CHANNEL2);
			}
		}
	}
}
